using System;
using System.Data;
using System.Configuration;
using System.Web;

using System.Data.OleDb;

namespace Com.Rf.Core
{
    public class DBHelper
    {
        public DBHelper()
        {
            Connection = new OleDbConnection(ConnectionString);
        }

        private string ConnectionString
        {
            get
            {
                string dbPath = System.Windows.Forms.Application.StartupPath + "/data.mdb";
                string connstr = "Provider=Microsoft.jet.OLEDB.4.0;Data Source=" + dbPath;
                return connstr;
            }
        }

        public OleDbConnection Connection = null;

        #region Execute

        public object ExecuteScalar(string sql)
        {
            OleDbCommand command = new OleDbCommand(sql, Connection);
            Connection.Open();
            object result = command.ExecuteScalar();
            Connection.Close();
            return result;
        }

        public int Execute(string sql)
        {
            OleDbCommand command = new OleDbCommand(sql, Connection);
            Connection.Open();
            int result = command.ExecuteNonQuery();
            Connection.Close();
            return result;
        }

        public int Execute(string sql, OleDbParameter[] parms)
        {
            OleDbCommand command = new OleDbCommand(sql, Connection);

            foreach (OleDbParameter parm in parms)
            {
                command.Parameters.Add(parm);
            }

            Connection.Open();
            int result = command.ExecuteNonQuery();
            Connection.Close();
            return result;
        }

        #endregion

        #region Query

        public DataSet Query(string sql)
        {
            OleDbCommand command = new OleDbCommand(sql, Connection);
            OleDbDataAdapter adapter = new OleDbDataAdapter(command);

            DataSet result = new DataSet();
            adapter.Fill(result);

            return result;
        }

        public DataSet Query(string sql, OleDbParameter[] parms)
        {
            OleDbCommand command = new OleDbCommand(sql, Connection);

            foreach (OleDbParameter parm in parms)
            {
                command.Parameters.Add(parm);
            }

            OleDbDataAdapter adapter = new OleDbDataAdapter(command);

            DataSet result = new DataSet();
            adapter.Fill(result);

            return result;
        }

        #endregion
    }

    
}
